import { defineStore } from 'pinia';
import { PermissionCode } from '@/types/global';

export const usePermissionStore = defineStore('permission', {
  state: () => ({
    permissions: [] as PermissionCode[],
  }),
  actions: {
    // 登录后把后端数组存进来
    setPermissions(codes: PermissionCode[]) {
      this.permissions = codes;
    },
    // 清除权限（退出登录）
    clear() {
      this.permissions = [];
    },
    // 判断是否有权限
    has(code: PermissionCode): boolean {
      return this.permissions.includes(code);
    },
  },
});